﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.Revit.Electric.Common;
using HYView;

namespace HYRElectric.Equipment
{
	[Regeneration(RegenerationOption.Manual)]
	[Transaction(TransactionMode.Manual)]
	internal class LeadSymbol : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			if (commandData == null)
			{
				throw new System.ArgumentNullException("commandData", "is null");
			}
			this.uiApp = commandData.Application;
			this.uiDoc = this.uiApp.ActiveUIDocument;
			this.doc = this.uiApp.ActiveUIDocument.Document;
			if (!(this.doc.ActiveView is ViewPlan))
			{
				Result result = SwitchView.SetUsableView((HYView.CmdViewType)3, commandData);
				if (result != Result.Succeeded)
				{
					return result;
				}
			}
			List<DbFamilyObject> dbFamilyObjects = new FamilyLibDataAssist(this.doc, "LeadSymbol").LoadFamilyDatas(null);
			List<BuiltInCategory> lstFilter = new List<BuiltInCategory>
			{
				(BuiltInCategory)(-2000150)
			};
			LeadSymbolForm leadSymbolForm = new LeadSymbolForm(dbFamilyObjects, this.doc);
			leadSymbolForm.Show(WindowHelper.HWndRevit);
			WindowHelper.SendMessageToWndRevit();
			for (;;)
			{
				try
				{
					new LeadSymbolLayout(this.uiDoc, this.doc, leadSymbolForm.SelFamily, null, lstFilter).Layout();
					if (leadSymbolForm.IsClick)
					{
						leadSymbolForm.IsClick = false;
						continue;
					}
				}
				catch (Exception ex)
				{
					YJKMessageBox.Show(ex.Message);
					leadSymbolForm.Close();
					return Autodesk.Revit.UI.Result.Failed;
				}
				break;
			}
			leadSymbolForm.Close();
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		private UIApplication uiApp;

		private Document doc;

		private UIDocument uiDoc;
	}
}
